import { lastValueFrom } from "rxjs";
import { Injectable } from "@nestjs/common";

import {
  Book,
  TonTpl,
  TonSku,
  RestDto,
  ENUM_APP,
  TonOrder,
  PageData,
  TonTplGroup,
  getSkuRpcDto,
  getTplRpcDto,
  putSaleRpcDto,
  TonOrderGroup,
  postSaleRpcDto,
  getOrderRpcDto,
  putAdjustRpcDto,
  getTplPageRpcDto,
  putProcessRpcDto,
  DisableUUid32Dto,
  postProcessRpcDto,
  getTplGroupRpcDto,
  putPurchaseRpcDto,
  postPurchaseRpcDto,
  getNotchStatRpcDto,
  putSaleReturnRpcDto,
  getOrderGroupRpcDto,
  TonTplRpcGetTplPath,
  TonSkuRpcGetSkuPath,
  TonSkuRpcPutSalePath,
  TonTplComRpcInterface,
  postSaleReturnRpcDto,
  TonSkuRpcPostSalePath,
  TonSkuReadRpcInterface,
  TonSkuRpcPutAdjustPath,
  TonTplRpcGetTplPagePath,
  TonSkuRpcPutProcessPath,
  TonOrderComRpcInterface,
  TonOrderRpcGetOrderPath,
  TonSkuRpcPostProcessPath,
  TonSkuRpcPutPurchasePath,
  TonSkuRpcPostPurchasePath,
  TonSkuRpcGetNotchStatPath,
  TonSkuRpcPutSaleReturnPath,
  TonOrderRpcDoBusyCheckPath,
  TonSkuRpcPostSaleReturnPath,
  TonSkuWriteRpcSaleInterface,
  TonOrderRpcDisableOrderPath,
  TonGroupRpcGetOrderGroupPath,
  TonTplGroupRpcGetTplGroupPath,
  TonSkuWriteRpcAdjustInterface,
  TonSkuWriteRpcProcessInterface,
  TonSkuWriteRpcPurchaseInterface,
  TonSkuWriteRpcSaleReturnInterface,
  doTonSkuCreationRes,
} from "qqlx-core";

import { BusRpc } from "./app.droplet-bus";
import { RpcWaiting } from "./rpc.response";
import { RpcConnection } from "./com.connection";

@Injectable()
//
export class RiverTonRpc
  extends RpcConnection
  implements
    TonTplComRpcInterface<RestDto>,
    TonOrderComRpcInterface<RestDto>,
    TonSkuReadRpcInterface<RestDto>,
    TonSkuWriteRpcPurchaseInterface<RestDto>,
    TonSkuWriteRpcAdjustInterface<RestDto>,
    TonSkuWriteRpcProcessInterface<RestDto>,
    TonSkuWriteRpcSaleInterface<RestDto>,
    TonSkuWriteRpcSaleReturnInterface<RestDto>
{
  constructor(readonly BusRpc: BusRpc) {
    super(BusRpc, ENUM_APP.RIVER_TON);
  }

  @RpcWaiting(TonTplGroupRpcGetTplGroupPath)
  async getTplGroup(dto: RestDto & getTplGroupRpcDto) {
    const ob = this.CLIENT.send(TonTplGroupRpcGetTplGroupPath, dto);
    return lastValueFrom<{ entityLs: TonTplGroup[] }>(ob);
  }

  @RpcWaiting(TonTplRpcGetTplPath)
  async getTpl(dto: RestDto & getTplRpcDto) {
    const ob = this.CLIENT.send(TonTplRpcGetTplPath, dto);
    return lastValueFrom<{ entityLs: TonTpl[] }>(ob);
  }

  @RpcWaiting(TonTplRpcGetTplPagePath)
  async getTplPage(dto: RestDto & getTplPageRpcDto) {
    const ob = this.CLIENT.send(TonTplRpcGetTplPagePath, dto);
    return lastValueFrom<PageData<TonTpl>>(ob);
  }

  //

  @RpcWaiting(TonGroupRpcGetOrderGroupPath)
  async getOrderGroup(dto: RestDto & getOrderGroupRpcDto) {
    const ob = this.CLIENT.send(TonGroupRpcGetOrderGroupPath, dto);
    return lastValueFrom<{ entityLs: TonOrderGroup[] }>(ob);
  }

  @RpcWaiting(TonOrderRpcGetOrderPath)
  async getOrder(dto: RestDto & getOrderRpcDto) {
    const ob = this.CLIENT.send(TonOrderRpcGetOrderPath, dto);
    return lastValueFrom<{ entityLs: TonOrder[] }>(ob);
  }

  //

  @RpcWaiting(TonSkuRpcGetSkuPath)
  async getSku(dto: RestDto & getSkuRpcDto) {
    const ob = this.CLIENT.send(TonSkuRpcGetSkuPath, dto);
    return lastValueFrom<{ entityLs: TonSku[] }>(ob);
  }

  @RpcWaiting(TonSkuRpcGetNotchStatPath)
  async getNotchStat(dto: RestDto & getNotchStatRpcDto) {
    const ob = this.CLIENT.send(TonSkuRpcGetNotchStatPath, dto);
    return lastValueFrom<{ total: number }>(ob);
  }

  //

  @RpcWaiting(TonOrderRpcDoBusyCheckPath)
  async doBusyCheck(dto: RestDto & { maskTask?: number }) {
    const ob = this.CLIENT.send(TonOrderRpcDoBusyCheckPath, dto);
    return lastValueFrom<{ count: number }>(ob);
  }

  @RpcWaiting(TonOrderRpcDisableOrderPath)
  async disableOrderSync(dto: RestDto & DisableUUid32Dto) {
    const ob = this.CLIENT.send(TonOrderRpcDisableOrderPath, dto);
    return lastValueFrom<{ updated: TonOrder; affected: TonSku[] }>(ob);
  }

  //

  @RpcWaiting(TonSkuRpcPutPurchasePath)
  async putPurchaseSync(dto: RestDto & putPurchaseRpcDto) {
    const ob = this.CLIENT.send(TonSkuRpcPutPurchasePath, dto);
    return lastValueFrom<doTonSkuCreationRes>(ob);
  }

  @RpcWaiting(TonSkuRpcPostPurchasePath)
  async postPurchaseSync(dto: RestDto & postPurchaseRpcDto) {
    const ob = this.CLIENT.send(TonSkuRpcPostPurchasePath, dto);
    return lastValueFrom<doTonSkuCreationRes>(ob);
  }

  @RpcWaiting(TonSkuRpcPutAdjustPath)
  async putAdjustSync(dto: RestDto & putAdjustRpcDto) {
    const ob = this.CLIENT.send(TonSkuRpcPutAdjustPath, dto);
    return lastValueFrom<doTonSkuCreationRes>(ob);
  }

  @RpcWaiting(TonSkuRpcPutProcessPath)
  async putProcessSync(dto: RestDto & putProcessRpcDto) {
    const ob = this.CLIENT.send(TonSkuRpcPutProcessPath, dto);
    return lastValueFrom<doTonSkuCreationRes>(ob);
  }

  @RpcWaiting(TonSkuRpcPostProcessPath)
  async postProcessSync(dto: RestDto & postProcessRpcDto) {
    const ob = this.CLIENT.send(TonSkuRpcPostProcessPath, dto);
    return lastValueFrom<doTonSkuCreationRes>(ob);
  }

  @RpcWaiting(TonSkuRpcPutSalePath)
  async putSaleSync(dto: RestDto & putSaleRpcDto) {
    const ob = this.CLIENT.send(TonSkuRpcPutSalePath, dto);
    return lastValueFrom<doTonSkuCreationRes>(ob);
  }

  @RpcWaiting(TonSkuRpcPostSalePath)
  async postSaleSync(dto: RestDto & postSaleRpcDto) {
    const ob = this.CLIENT.send(TonSkuRpcPostSalePath, dto);
    return lastValueFrom<doTonSkuCreationRes>(ob);
  }

  @RpcWaiting(TonSkuRpcPutSaleReturnPath)
  async putSaleReturnSync(dto: RestDto & putSaleReturnRpcDto) {
    const ob = this.CLIENT.send(TonSkuRpcPutSaleReturnPath, dto);
    return lastValueFrom<doTonSkuCreationRes>(ob);
  }

  @RpcWaiting(TonSkuRpcPostSaleReturnPath)
  async postSaleReturnSync(dto: RestDto & postSaleReturnRpcDto) {
    const ob = this.CLIENT.send(TonSkuRpcPostSaleReturnPath, dto);
    return lastValueFrom<doTonSkuCreationRes>(ob);
  }
}
